package com.baimo.support.utils;

/**
 * Copyright © 2012 Newline Information & Technology Co., Ltd.
 */

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
	public static final char UNDERLINE='_';

	/**
	 * integer [1, MAX)
	 */
	public final static String REGEX_POSITIVE_INTEGER = "^\\+?[1-9]\\d*$"; //$NON-NLS-1$



	public static String camelToUnderline(String param){
		if (param==null||"".equals(param.trim())){
			return "";
		}
		int len=param.length();
		StringBuilder sb=new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c=param.charAt(i);
			if (Character.isUpperCase(c)){
				sb.append(UNDERLINE);
				sb.append(Character.toLowerCase(c));
			}else{
				sb.append(c);
			}
		}
		return sb.toString();
    }

	public static String underlineToCamel(String param){
		if (param==null||"".equals(param.trim())){
			return "";
		}
		int len=param.length();
		StringBuilder sb=new StringBuilder(len);
		for (int i = 0; i < len; i++) {
			char c=param.charAt(i);
			if (c==UNDERLINE){
				if (++i<len){
					sb.append(Character.toUpperCase(param.charAt(i)));
				}
			}else{
				sb.append(c);
			}
		}
		return sb.toString();
	}

	public static String underlineToCamel2(String param){
		if (param==null||"".equals(param.trim())){
			return "";
		}
		StringBuilder sb=new StringBuilder(param);
		Matcher mc= Pattern.compile("_").matcher(param);
		int i=0;
		while (mc.find()){
			int position=mc.end()-(i++);
            //String.valueOf(Character.toUpperCase(sb.charAt(position)));
			sb.replace(position-1,position+1,sb.substring(position,position+1).toUpperCase());
		}
		return sb.toString();
	}
		
	
	
	
	public interface Regex {
		static String MAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
		static String CHINESE_CHARACTERS = "^[\u4e00-\u9fa5]{0,}$";
		static String INTER_URL = "^http[s]?://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";
		static String TELEPHONE = "^(\\d{3,4}-)?\\d{7,8}$";
		static String MOBILE = "^[1][3,4,5,7,8]+\\d{9}$";
		static String BLANK = "^\\s*|\\s*$";
		static String HTML_TAG = "<(\\S*?)[^>]*>.*?</\1>|<.*? />";
		static String IP = "^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
		static String POST_CODE = "^[1-9]\\d{5}$";
		static String ID_CARD = "\\d{15}|\\d{18}";
		static String IS_NUMBER = "[0-9]*";
	}
	   public static boolean isEmpty(String str) {
	        return str == null || str.length() == 0;
	    }
	
	   public static boolean isNotEmpty(String str) {
	        return !StringUtils.isEmpty(str);
	    }

	public static boolean isNull(String value) {
		return value == null || value.trim().length() == 0;
	}

	public static boolean isEmail(String value, int length) {
		return !isNull(value) && value.matches(Regex.MAIL)
				&& value.length() <= length;
	}


	public static boolean isEmail(String value) {
		return !isNull(value) && value.matches(Regex.MAIL);
	}


	public static boolean isChineseCharacters(String value) {
		return !isNull(value)
				&& value.matches(Regex.CHINESE_CHARACTERS);
	}

	public static boolean isInterUrl(String value) {
		return !isNull(value) && value.matches(Regex.INTER_URL);
	}

	public static boolean isTelephone(String value) {
		return !isNull(value) && value.matches(Regex.TELEPHONE);
	}


	public static boolean isMobile(String value) {
		return !isNull(value) && value.matches(Regex.MOBILE);
	}

	public static boolean headEndBlank(String value) {
		return !isNull(value) && value.matches(Regex.BLANK);
	}

	public static boolean hasHtmlTag(String value) {
		return !isNull(value) && value.matches(Regex.HTML_TAG);
	}

	public static boolean isIP(String value) {
		return !isNull(value) && value.matches(Regex.IP);
	}

	public static boolean isPostCode(String value) {
		return !isNull(value) && value.matches(Regex.POST_CODE);
	}

	public static boolean isIDCard(String value) {
		return !isNull(value) && value.matches(Regex.ID_CARD);
	}

	public static String copy(String value, int count) {
		if (isNull(value)) {
			throw new NullPointerException("输入字符�?:" + value);
		}
		for (int i = 0; i < count; i++) {
			value += value;
		}
		return value;
	}

	public static String delete(String valueStr, String delStr) {
		if (isNull(valueStr)) {
			throw new NullPointerException("输入字符�?:");
		}
		if (isNull(delStr)) {
			delStr = "null";
		}
		return valueStr.replace(delStr, "");
	}

	public static String insert(String valueStr, String inStr, int offset) {
		if (isNull(valueStr)) {
			throw new NullPointerException("输入字符�?:" + valueStr);
		}
		if (offset < 0 || offset > valueStr.length()) {
			throw new StringIndexOutOfBoundsException(offset);
		}
		if (isNull(inStr)) {
			inStr = "null";
		}
		return valueStr.substring(0, offset) + inStr
				+ valueStr.substring(offset);
	}

	public static String encryptionByMD5(String md5) {
		StringBuffer sb = new StringBuffer();
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] array = md.digest(md5.getBytes());
			for (int i = 0; i < array.length; ++i) {
				sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100)
						.substring(1, 3));
			}
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return sb.toString().trim();
	}


	private static boolean isMatch(String regex, String orginal) {
		if (orginal == null || orginal.trim().equals("")) { //$NON-NLS-1$
			return false;
		}
		Pattern pattern = Pattern.compile(regex);
		Matcher isNum = pattern.matcher(orginal);
		return isNum.matches();
	}
	/**
	 *   正整数[1,MAX)
	 *
	 * @param orginal
	 * @Description: 是否为正整数
	 * @return boolean
	 */
	public static boolean isPositiveInteger(String orginal) {
		return isMatch(REGEX_POSITIVE_INTEGER, orginal);
	}

	public static void main(String args[]){
		System.out.println(isPositiveInteger(null));
	}

	/**
	 * 将String转换成Int 默认是0
	 * @param val
	 * @return
	 */
	public static int formatStringToInt(String val){
		return StringUtils.isEmpty(val) ? 0 : isPositiveInteger(val) ? Integer.parseInt(val) : 0;
	}

}
